<?php
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');
require_once('Model/DB.php');
require_once('Model/Validate.php');
require_once('Model/Pager.php');
require_once('Model/Uploader.php');
require_once('Plugin/News.php');

$sys = new Model_SiteManage();
$sys->checkLogin();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
$news   = new Model_news();
$smarty->assign('news', $news);

if (isset($params['do']) && function_exists('exec_' . $params['do'])){
   $params['op'] = call_user_func('exec_' . $params['do'], &$params, &$smarty, &$news);
}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], $params, &$smarty, &$news);
}
else{
    call_user_func('op_search', $params, &$smarty, &$news);
}
//
// 検索・一覧
function op_search($params, &$smarty, &$news)
{
    if (!isset($params['search'])){
        $params['search']   = array();
    }else{
        $page_id = isset($params['page_id']) && $params['page_id'] > 1 ? $params['page_id'] : 1;
        $max_row = 20;
        $pager   = new Model_Pager();
        $pager->getPagerInfo($news->getListCount($params['search']), $page_id, $max_row);
        $list = $news->getList($params['search'], 'id DESC', $max_row, ($max_row * ($page_id - 1)));
        $smarty->assign('list', $list);
        $smarty->assign('pager', $pager);
    }
    $smarty->assign('status', $news->getStatusMap());
    $smarty->assign('type', $news->getTypeMap());
    $smarty->assign('search', $params['search']);
    $smarty->display('news/news_index.html');
}
// 新規登録
function op_regist($params, &$smarty, &$news)
{
    if (!isset($params['forms'])){
        $forms = array();
        foreach($news->getFormKeys() as $key){
            $forms[$key] = '';
        }
        $forms["date"]   = date("Y-m-d");
        $forms["status"] = 2;
        $forms["type"]   = 0;
        $params['forms'] = $forms;
    }
    global $sys;
    if ($sys->isValidPlugin('MANAGE_CATEGORY')){
        $setting = $news->getSetting();
        $smarty->assign('use_category', $setting['category_id']);
    }
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('news/news_form.html');
}
function exec_validate_regist_form(&$params, &$smarty, &$news)
{
    $err = array();
    if ($news->validate($params['forms'], $err)){
        return exec_regist_form($params, $smarty, $news);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'regist';
}
function exec_regist_form(&$params, &$smarty, &$news)
{
    if(isset($params['forms']['file1'])){
        $tmp_file1 = $params['forms']['file1'];
        unset($params['forms']['file1']);
    }
    if(isset($params['forms']['file2'])){
        $tmp_file2 = $params['forms']['file2'];
        unset($params['forms']['file2']);
    }
    $params['forms']['st_time'] = $params['forms']['st_date'].' '.$params['forms']['st_time'];
    $params['forms']['ed_time'] = $params['forms']['ed_date'].' '.$params['forms']['ed_time'];
    $id = $news->insert($params['forms']);
    $uploader  = new Model_Uploader();
    if(isset($tmp_file1)){
        $cache         = $tmp_file1[0];
        $base_name     = 'image_news_'.$id;
        $move_to       = $news->getThumbDir();
        $filename      = $uploader->moveCacheImage($cache, $move_to, $base_name);
        $data          = array();
        $data['file1'] = $filename;
        $news->update($id, $data);
    }
    if($params['forms']['type'] == 2 && isset($tmp_file2)){
        $cache         = $tmp_file2[0];
        $base_name     = sprintf('%06d', $id);
        $move_to       = $news->getFileDir();
        $filename      = $uploader->moveCacheFile($cache, $move_to, $base_name);
        $data          = array();
        $data['file2'] = $filename;
        $news->update($id, $data);
    }
    $news->updateCategory($id, $params['forms']['select_category']);
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=regist_complete&id=' . $id);
    exit();
}
function op_regist_complete($params, &$smarty, &$news)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $forms = $news->get($params['id']);
        $smarty->assign('forms', $forms);
    }
    $smarty->assign('message', '登録が完了しました');
    $smarty->display('news/news_form.html');
}
// 編集
function op_edit($params, &$smarty, &$news)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
    if (!isset($params['forms'])){
        $forms = $news->get($id);
        if ($forms['file1'] != ''){
            $forms['file1'] = array(0 => $news->getThumbUrl() . $forms['file1'] );
        }
        if ($forms['file2'] != ''){
            $forms['file2_saved'] = array(0 => $forms['file2'] );
        }
        if($forms['st_time'] != '0000-00-00 00:00:00'){
            $forms['st_date'] = date('Y-m-d', strtotime($forms['st_time']));
            $forms['st_time'] = date('G', strtotime($forms['st_time']));
        }
        if($forms['ed_time'] != '0000-00-00 00:00:00'){
            $forms['ed_date'] = date('Y-m-d', strtotime($forms['ed_time']));
            $forms['ed_time'] = date('G', strtotime($forms['ed_time']));
        }
        $params['forms'] = $forms;
    }
    global $sys;
    if ($sys->isValidPlugin('MANAGE_CATEGORY')){
        $setting = $news->getSetting();
        $smarty->assign('use_category', $setting['category_id']);
        $params['forms']['select_category'] = $news->getCategory($id);
    }
    $smarty->assign('id', $id);
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('news/news_form.html');
}
function exec_validate_edit_form(&$params, &$smarty, &$news)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    if ($params['mode'] == 'delete')
    {
        return exec_delete_form($params, $smarty, $news);
    }
    $err = array();
    if ($news->validate($params['forms'], $err)){
        return exec_edit_form($params, $smarty, $news);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'edit';
}
function exec_edit_form(&$params, &$smarty, &$news)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
    if(isset($params['forms']['file1'])){
        $tmp_file1 = $params['forms']['file1'];
        unset($params['forms']['file1']);
    }
    if(isset($params['forms']['file2'])){
        $tmp_file2 = $params['forms']['file2'];
        unset($params['forms']['file2']);
    }    
    $params['forms']['st_time'] = $params['forms']['st_date'].' '.$params['forms']['st_time'];
    $params['forms']['ed_time'] = $params['forms']['ed_date'].' '.$params['forms']['ed_time'];
    $news->update($id, $params['forms']);
    $uploader  = new Model_Uploader();
    if(isset($tmp_file1) && !preg_match('/^http/', $tmp_file1[0])){
        $cache         = $tmp_file1[0];
        $base_name     = sprintf('%06d', $id);
        $move_to       = $news->getThumbDir();
        $filename      = $uploader->moveCacheImage($cache, $move_to, $base_name);
        $data          = array();
        $data['file1'] = $filename;
        $news->update($id, $data);
    }
    if($params['forms']['type'] == 2 && isset($tmp_file2)){
        $cache         = $tmp_file2[0];
        if(!empty($cache)){
            $base_name     = sprintf('%06d', $id);
            $move_to       = $news->getFileDir();
            $filename      = $uploader->moveCacheFile($cache, $move_to, $base_name);
            $data          = array();
            $data['file2'] = $filename;
            $news->update($id, $data);
        }
    }
    $news->updateCategory($id, $params['forms']['select_category']);
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=edit_complete&id=' . $id);
    exit();
}
function op_edit_complete($params, &$smarty, &$news)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
    }
    $smarty->assign('message', '更新が完了しました');
    $smarty->display('news/news_form.html');
}
//　削除
function exec_delete_form(&$params, &$smarty, &$news)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $row = $news->get($params['id']);
    $res = $news->delete($params['id']);
    if($res){
        if( $row['file1'] != '' ){
            $result = unlink( $news->getSiteDir().'images/' . $row['file1'] );
        }
        if( $row['file2'] != '' ){
            unlink( $news->getSiteDir().'images/' . $row['file2'] );
        }
        $news->updateCategory($id, array());
    }
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=delete_complete&id=' . $id);
    exit();
}
function op_delete_complete($params, &$smarty, &$news)
{
    $smarty->assign('phase', 'complete');
    $smarty->assign('message', '削除が完了しました');
    $smarty->display('news/news_form.html');
}
// 基本設定
function op_setting($params, &$smarty, &$news)
{
    if (!isset($params['forms'])){
        $params['forms'] = $news->getSetting();
    }
    global $sys;
    if ($sys->isValidPlugin('MANAGE_CATEGORY')){
        require_once('Plugin/Category.php');
        $category = new Model_Category();
        $list = $category->getList(array('parent' => 1, 'site_id' => $sys->getCurrentSite()));
        foreach($list as $one)
        {
            $category_list[$one['id']] = $one['category_name'];
        }
        $smarty->assign('category_list', $category_list);
    }
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $params['forms']);
    $smarty->display('news/news_setting.html');
}
function exec_validate_setting_form(&$params, &$smarty, &$news)
{
    $err = array();
    if ($news->validateSetting($params['forms'], $err)){
        return exec_setting_form($params, $smarty, $news);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'setting';
}
function exec_setting_form(&$params, &$smarty, &$news)
{
    if (empty($params['forms']['category_id'])){
        $params['forms']['category_id'] = '0';    // default
    }
    $params['forms']['top_items']  = implode(',', $params['forms']['top_items']);
    $params['forms']['list_items'] = implode(',', $params['forms']['list_items']);
    $news->updateSetting($params['forms']);
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=setting_complete');
    exit();
}
function op_setting_complete($params, &$smarty, &$news)
{
    $smarty->assign('phase', 'complete');
    $smarty->assign('message', '設定が完了しました');
    $smarty->display('news/news_setting.html');
}
?>